﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using VideoClassLibrary.BusinessLogicLayer;

public partial class NewsManagement_ViewNewsList : AdminUserBase
{
    public string PublishedStatus;
    public string exeJs = string.Empty;
    public string HtmlNEWS;
    protected void Page_Load(object sender, EventArgs e)
    {
        PublishedStatus = Request.QueryString["IsPublished"];
        if (!this.IsPostBack)
        {
            bindPageData();
        }
        ClientScript.RegisterStartupScript(typeof(string), "exejs", "<script>" + exeJs + "</script>");
    }

    protected void pager_PageChanged(object sender, EventArgs e)
    {
        bindPageData();
    }

    private void bindPageData()
    {
        string str = " and IsPublished=1";
        exeJs = "swithTab('tab1')";
        if (!string.IsNullOrEmpty(PublishedStatus) && int.Parse(PublishedStatus) == 0)
        {
            str = "  and  IsPublished=0";
            exeJs = "swithTab('tab2')";
        }
        int schoolid = this.CurrentUser.SchoolID;
        NewsModule NM = new NewsModule();
        DataSet ds = NM.GetNewsListByWhere(schoolid, str);
        this.repNews.DataSource = this.getPage(ds);
        this.repNews.DataBind();
    }

    public PagedDataSource getPage(DataSet ds)
    {
        this.pager.RecordCount = ds.Tables[0].Rows.Count;
        PagedDataSource pds = new PagedDataSource();
        pds.DataSource = ds.Tables[0].DefaultView;
        pds.AllowPaging = true;
        pds.CurrentPageIndex = pager.CurrentPageIndex - 1;
        pds.PageSize = pager.PageSize;
        return pds;
    }
    #region
    
    protected void repNews_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

        if (e.CommandName.ToLower() == "setispublished")
        {
            string argValue = e.CommandArgument.ToString();
            NewsModule nm = new NewsModule();
            nm.UpdateNewsIsPublished(this.SchoolID, argValue.Split('|')[1] == "0" ? 1 : 0, int.Parse(argValue.Split('|')[0]));
        }
        Response.Redirect("ViewNewsList.aspx");
    }


    protected void repNews_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            DataRowView drv = (DataRowView)e.Item.DataItem;

            string IsPublished = drv["IsPublished"].ToString();
            int schoolid = this.CurrentUser.SchoolID;
            LinkButton btnIsPublished = (LinkButton)e.Item.FindControl("btnIsPublished");
            btnIsPublished.Text = (IsPublished == "1" ? "禁用" : "发布");
            string NewsContent = drv["NewsContent"].ToString();
            Label lblNewsContent = e.Item.FindControl("lblNewsContent") as Label;
            lblNewsContent.Text = PageHelper.NoHTML(NewsContent);
        }
        //if (news() == 4)
        //{
        //    LinkButton lbtn = (LinkButton)e.Item.FindControl("btnIsPublished");
        //    lbtn.Visible = false;
        //}

    }
    /// <summary>
    /// 根据选择删除数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDelete_Command(object sender, CommandEventArgs e)
    {

        int id = int.Parse(e.CommandArgument.ToString());
        NewsModule nm = new NewsModule();
        nm.DeleteNewsbyNid(id);
        Response.Redirect("ViewNewsList.aspx");
    }
    /// <summary>
    /// 修改新闻
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public void btnUpdate_Click(object sender, CommandEventArgs e)
    {
        Response.Redirect("UpdateNews.aspx?NID=" + e.CommandArgument.ToString());
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        Response.Redirect("AddNews.aspx");
    }
    #endregion
    ////已发布
    //protected void lbtnpublish_Click(object sender, EventArgs e)
    //{
    //    bindPageData();
    //}
    //待审核
    //protected void lbtnNot_Click(object sender, EventArgs e)
    //{
    //    string str = " and IsPublished=0";
    //    int schoolid = this.CurrentUser.SchoolID;
    //    NewsModule NM = new NewsModule();
    //    DataSet ds = NM.GetNewsListByWhere(schoolid, str);
    //    this.repNews.DataSource = this.getPage(ds);
    //    this.repNews.DataBind();
    //}
    //当返回值为4时，隐藏控件按钮btnIsPublished
    private int news()
    {
        DataSet ds = new DataSet();
        AdminModule am = new AdminModule();
        string types = AdminType.ToString();
        ds = am.GetAdmin(-1, types);
        DataRow dr = ds.Tables[0].Rows[0];
        int result = Convert.ToInt32(dr["VA_type"]);
        return result;
    }
}
